package com.mm.update.service;

import com.mm.update.util.SftpClient;
import com.mm.update.vo.DeployLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ClassName: VosDeployService
 * Description:
 * date: 2023/5/22 16:34
 *
 * @author  
 * @since JDK 1.8
 */
@Service
@Slf4j
public class VosDeployService {
    @Value("${shellName}")
    private String shellName;

    @Value("${logging.file.path}")
    private String path;
    List<DeployLog> deployLogList = new ArrayList<>();

    public void uploadJar(String ip, String username, String password, MultipartFile file) throws Exception {
        if(ip.contains("&")){
            String[] split = ip.split("&");
            for (String s : split) {
                SftpClient client = new SftpClient(s, username, password);
                client.uploadFile("/opt/mm", file.getInputStream(), file.getOriginalFilename());
            }
        }else{
            SftpClient client = new SftpClient(ip, username, password);
            client.uploadFile("/opt/mm", file.getInputStream(), file.getOriginalFilename());
        }
    }

    public void uploadAndDeployJar(String ip, String username, String password, MultipartFile file,DeployLog log) throws Exception {
        FixedSizeQueue.addElement(log);
        SftpClient client = new SftpClient(ip, username, password);
        client.uploadFile("/opt/mm", file.getInputStream(), file.getOriginalFilename());
        deploy(ip,username,password,file.getOriginalFilename());
    }

    public String deploy(String ip, String username, String password, String jarName) throws Exception {
        SftpClient client = new SftpClient(ip, username, password);
        String cmd = "cd /opt/mm;sh " + shellName + " " + jarName;
        return client.executeCmd(cmd);
    }

    public void uploadVos(String ip, String username, String password,boolean isNewCti, MultipartFile file) throws Exception {
        SftpClient client = new SftpClient(ip, username, password);
        if(isNewCti){
            client.uploadFile("/opt/mmPackage", file.getInputStream(), file.getOriginalFilename());
        }else{
            client.uploadFile("/home/samba", file.getInputStream(), file.getOriginalFilename());
        }

    }

    public String deployVos(String ip, String username, String password,boolean isNewCti, String jarName) throws Exception {

        SftpClient client = new SftpClient(ip, username, password);
        if (isNewCti) {
            String cmd = "cd /opt/apache-tomcat-8.5.79/webapps;sh restartTomcat8.sh " + jarName;
            return client.executeCmd(cmd);
        } else {
            String cmd = "cd /opt/jboss-eap-6.1/standalone/deploys/cti;sh restartCti.sh " + jarName;
            return client.executeCmd(cmd);

        }
    }

    public void uploadBatch(String ip, String username, String password, List<MultipartFile> fileList) throws Exception {
        SftpClient client = new SftpClient(ip, username, password);
        client.uploadBatch("/opt/mm", fileList);
    }

    public String deployBatch(String ip, String username, String password) throws Exception {
        SftpClient client = new SftpClient(ip, username, password);
        String cmd = "cd /opt/mm;sh restartall.sh";
        return client.executeCmd(cmd);
    }

    public List<DeployLog> getDeployList() {
        String inputFilePath = path+"/vosupdatelog.log"; // 替换为实际的输入文件路径
        List<DeployLog> deployLogs = new ArrayList<>();
        List<String> lines = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
        ) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int start = Math.max(0, lines.size() - 100);
        lines = lines.subList(start, lines.size());
        Collections.reverse(lines);
        for (String line : lines) {
            try {
                String[] split = line.split("\\|");
                DeployLog deployLog = new DeployLog(split[0], split[3], split[4], split[2], split[1]);
                deployLogs.add(deployLog);
            } catch (Exception e) {
                log.error("解析日志出错");
            }
        }

        return deployLogs;
    }
    public List<DeployLog> getDeployList1() {

        return FixedSizeQueue.getReversedList();
    }
}
